<?php
include_once "config.php";
include_once "db.php";
include_once "valida.php";
include_once "class_assunto.php";
include_once "class_despachante.php";
include_once "class_evento.php";
include_once "class_log.php";
include_once "class_protocolo.php";
include_once "class_setor.php";
include_once "class_status.php";
include_once "class_arquivo.php";

function logar(){
    $login = addslashes($_POST["usuario_ci"]);
    $senha = md5($_POST["senha_ci"]);
    
    if(valida::nome($login)){
        $r = 0;
        $sql = "select * from ci_despachante where login = '$login' and senha = '$senha'";
        $db = new db(config::$driver);
        $conexao = $db->conecta();
        $result = $db->query($sql, $conexao);
        $usuario = $db->fetch_array($result);
        
        if($usuario["id"] != ''){
            $_SESSION["ci_user_id"]     = $usuario["id"];
            $_SESSION["ci_user_titulo"] = $usuario["titulo"];
            $_SESSION["ci_user_nome"]   = $usuario["nome"];
            $_SESSION["ci_user_setor"]  = $usuario["id_setor"];
            $_SESSION["ci_user_login"]  = $usuario["login"];
            $_SESSION["ci_user_email"]  = $usuario["email"];
            $_SESSION["ci_user_nivel"]  = $usuario["nivel"];
            $r = 1;
        }
    }
    
    if($r == 1){
        $log = new log('', '', "Usiário $login efetuou login");
        $log->salvar();
        unset($log);
    }else{
        $log = new log('', '', "Usiário $login tentou efetuar login");
        $log->salvar();
        unset($log);
    }
    
    return $r;
}

function logado(){
    if($_SESSION["ci_user_id"]  != NULL && 
    $_SESSION["ci_user_nome"]   != NULL && 
    $_SESSION["ci_user_setor"]  != NULL && 
    $_SESSION["ci_user_login"]  != NULL && 
    $_SESSION["ci_user_nivel"]  != NULL){
        return 1;
    }else{
        return 0;
    }
}

function deslogar(){
    $_SESSION["ci_user_id"]     = NULL;
    $_SESSION["ci_user_titulo"] = NULL;
    $_SESSION["ci_user_nome"]   = NULL;
    $_SESSION["ci_user_setor"]  = NULL;
    $_SESSION["ci_user_login"]  = NULL;
    $_SESSION["ci_user_email"]  = NULL;
    $_SESSION["ci_user_nivel"]  = NULL;
    
    $log = new log('', '', "O usiário ".$_SESSION["ci_user_login"]." desconectou");
    $log->salvar();
    unset($log);
    
    header("location: index.php");
}

function gera_senha(){
    $senha = '';
    for($i = 0; $i < 6; $i++){
        $t = rand(0,2);
        if($t == 0){
            $senha .= rand(0,9);
        }else{
            $senha .= chr(rand(97,122));
        }
    }
    return $senha;
}

function data_ptbr($data){
    $ano = substr($data, 0, 4);
    $mes = substr($data, 5, 2);
    $dia = substr($data, 8, 2);
    
    return $dia."/".$mes."/".$ano;
}

function IP_Uninga(){
    if($_SERVER["REMOTE_ADDR"] == ""){
        return 1;
    }else{
        return 0;
    }
}

function contaCI($status){
    
    $sql = "SELECT DISTINCT p.id FROM ci_protocolo p";
    if($_SESSION["ci_user_nivel"] == 2){
        $sql .= ", ci_despachante d WHERE p.id_destinatario = '".$_SESSION["ci_user_id"]."'";
    }

    $db = new db(config::$driver);
    $conexao = $db->conecta();
    $result = $db->query($sql, $conexao);

    $cont = 0;
    while($ids = $db->fetch_array($result)){
        $sql_eve = "select * from ci_evento where id_protocolo = '".$ids[0]."' order by id desc limit 1";
        $res_eve = $db->query($sql_eve, $conexao);
        $eve = $db->fetch_array($res_eve);

        if($eve["lido"] == 0 && $eve["id_destinatario"] == $_SESSION["ci_user_id"]){
            switch ($status){
                case 0 : if(!in_array($eve["id_status"], array(54,55,58))){ $cont++; } break;
                case 54: if($eve["id_status"] == 54){ $cont++; } break;
                case 55: if($eve["id_status"] == 55){ $cont++; } break;
                case 58: if($eve["id_status"] == 58){ $cont++; } break;
            }
        }
    }
    $db->close($conexao);
            
    if($cont > 0){
        return " <strong>(".$cont.")</strong>";
    }else{
        return NULL;
    }
}
?>
